package Stack;

import org.w3c.dom.NodeList;

import java.util.*;


//最小栈
class MinStack {
    public Stack<Integer> stack;
    public Stack<Integer> minstack;
    public MinStack() {
        stack =new Stack<>();
        minstack =new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(minstack.empty()){
            minstack.push(val);

        }else{
            int peekval =minstack.peek();
            if(val<=peekval){
                minstack.push(val);
            }
        }
    }

    public void pop() {
        if(stack.empty()){
            return;
        }
        int popval =stack.pop();
        if(popval==minstack.peek()){
            minstack.pop();
        }
    }

    public int top() {
        if(stack.empty()){
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        if(stack.empty()){
            return -1;
        }
        return minstack.peek();
    }
}
public class Test {
    /*
     栈的压入、弹出序列
     */
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack =new Stack<>();
        int j=0;
        for (int i=0;i<pushV.length;i++){
            stack.push(pushV[i]);
            while(!stack.empty()&&j<pushV.length&&stack.pop()==popV[j]){
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }


    //中缀表达式
    /*
    你一个字符串数组 tokens ，表示一个根据 逆波兰表示法 表示的算术表达式。

请你计算该表达式。返回一个表示表达式值的整数。
     */
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String str: tokens){
            int x =Integer.parseInt(str);
            stack.push(x);
            if(!isOperator(str)){
                stack.pop();
            }else{
                int val1 =stack.pop();
                int val2 =stack.pop();
                switch (str){
                    case "+":
                        stack.push(val2+val1);
                        break;
                    case "-":
                        stack.push(val2-val1);
                        break;
                    case "*":
                        stack.push(val2*val1);
                        break;
                    case "/":
                        stack.push(val2/val1);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private  boolean isOperator(String ch){
        if(ch.equals("+")||ch.equals("-")||ch.equals("*")||ch.equals("/")){
            return  true;
        }
        return false;
    }
    /*
    给定一个只包括 '('，')'，'{'，'}'，'['，']' 的字符串 s ，判断字符串是否有效。

有效字符串需满足：

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
每个右括号都有一个对应的相同类型的左括号。
     */
    public boolean isValid(String s) {
        Stack<Character> stack =new Stack<>();
        for (int i=0;i<s.length();i++){
            char ch =s.charAt(i);
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else{
                if(stack.isEmpty()){
                    return  false;
                }
                //判断值是否匹配
                char ch2 =stack.peek();
                if(ch==')'&&ch2=='('||ch=='}'&&ch2=='{'||ch==']'&&ch2=='['){
                    stack.pop();
                }else{
                    return  false;
                }
            }
        }
        if(!stack.isEmpty()){
            return false;
        }
        return  true;
    }
    public static void main3(String[] args) {
        Queue< Integer> queue =new LinkedList<>();
        LinkedList<Integer> queue2 =new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        queue.offer(5);
        System.out.println(queue.size());//查看元素个数
        System.out.println(queue.peek());//查看队头元素
        queue.poll();
        System.out.println(queue.poll());
        if(queue.isEmpty()){
            System.out.println("队列空");
        }else{
            System.out.println(queue.size());
        }
    }
    public static void main2(String[] args) {
        MyStack stack =new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }
    public static void main1(String[] args) {
        Stack<Integer> stack =new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        System.out.println(stack.size());
        System.out.println(stack.peek());
        //System.out.println(stack);
        stack.pop();
        //System.out.println(stack.search(2));
        if(stack.empty()){
            System.out.println("栈空");
        }else{
            System.out.println(stack.size());
        }
    }
}
